/*
 * @Author: 45F
 * @Date: 2019-07-24 10:02:24
 * @LastEditors: 45F
 * @LastEditTime: 2019-09-04 16:53:38
 * @Description: 路由
 */

import Vue from "vue";
import Router from "vue-router";
import routes from "./routers";
import { getToken, getUser } from "@/libs/util";
import config from "@/config";
const { masterHomeName, userHomeName } = config;

Vue.use(Router);

const router = new Router({
  routes,
  base: process.env.BASE_URL, // 如果部署到非域名根目录，还需要配置base选项为前文配置的publicPath值
  mode: "hash"
  // mode: 'history'
});
// console.log('BASE_URL:'+process.env.BASE_URL)
const LOGIN_PAGE_NAME = "login";
// const LOGIN_PAGE_NAME = 'zdjqjtsn'
const HOME = "data_exchange";

// router.beforeEach((to, from, next) => {
//   let port = location.port;
//   const token = getToken(port);
//   // const token = 1123
//   const user = getUser();
//   // debugger
//   if (!token && to.name !== LOGIN_PAGE_NAME) {
//     // 未登录且要跳转的页面不是登录页
//     next({
//       name: LOGIN_PAGE_NAME // 跳转到登录页
//     });
//   } else if (token && to.name === LOGIN_PAGE_NAME) {
//     // 已登录且要跳转的页面是登录页
//     next({
//       name: HOME
//     });
//   } else if (to.name === HOME) {
//     // 已登录且跳转的页面是首页
//     let name;
//     if (user.userType === 1) { // 1是管理员权限
//       name = masterHomeName;
//     } else {
//       name = userHomeName;
//     }
//     next({
//       name: name
//     });
//   } else {
//     next();
//   }
//   // debugger;
// });

router.afterEach(to => {
  window.scrollTo(0, 0);
});

export default router;
